package com.ysxm.service.adminservice.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysxm.dao.adminmapper.AdminDao;
import com.ysxm.entity.Admin;
import com.ysxm.service.adminservice.AdminService;
import com.ysxm.util.CustomException;

@Service
@Transactional
public class AdminServiceImpl implements AdminService {
	// 注入CustomerDao
	@Autowired
	private AdminDao adminDao;

	public Admin findAdminById(Integer aid) {
		try {
			adminDao.findAdminById(aid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("查詢失败");
		}
		return adminDao.findAdminById(aid);

	}

	// 登录
	@SuppressWarnings("unused")
	public Admin adLogin(String aname, String apassword,HttpSession session) {
		/*if (aname.equals("")) {
			throw new CustomException("管理员名不能为空");
		}
		if (apassword.equals("")) {
			throw new CustomException("密码不能为空");
		}*/
		try {
			Admin admin = adminDao.adLogin(aname, apassword);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("管理员名或密码有误");
		}
		Admin account= adminDao.adLogin(aname, apassword);
		session.setAttribute("account", account);
		return account;
	}

	// 添加管理员
	public boolean addAdmin(Admin admin) {

		if (admin.getAname() == null || admin.getAname().equals("")) {
			throw new CustomException("管理员名不能为空");
		}
		if (admin.getApassword() == null || admin.getApassword().equals("")) {
			throw new CustomException("密码不能为空");
		}
		if (admin.getAphone() == null || admin.getAphone().equals("")) {
			throw new CustomException("手机不能为空");
		}
		try {
			adminDao.addAdmin(admin);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("添加失败");
		}
		return false;
	}

	public boolean updateAdmin(Admin admin) {

		if (admin.getAname() == null || admin.getAname().equals("")) {
			throw new CustomException("管理员名不能为空");
		}
		// 判断管理员是否重复
		boolean b;
		if (adminDao.selectAdminByName(admin.getAname()) == null) {
			b = true;
		} else {
			b = false;
		}
		if (admin.getApassword() == null || admin.getApassword().equals("")) {
			throw new CustomException("密码不能为空");
		}
		if (admin.getAphone() == null || admin.getAphone().equals("")) {
			throw new CustomException("手机不能为空");
		}
		if (!b) {
			throw new CustomException("管理员名已存在");
		}
		try {
			adminDao.updateAdmin(admin);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("管理员名或密码有误");
		}
		return b;

	}

	// 刪除管理員
	public void deleteAdmin(Integer aid) {
		if (aid == null || aid.equals("")) {
			throw new CustomException("查無此人，请重试");
		}
		try {
			adminDao.deleteAdmin(aid);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除失败，请重试");
		}

	}

	public List<Admin> findAllAdmin(int pageNum, int pageSize) {
		List<Admin> alist = adminDao.findAllAdmin(pageNum, pageSize);

		if (alist.size() == 0) {
			throw new CustomException("未有管理员，请添加");
		}
		try {
			@SuppressWarnings("unused")
			PageInfo<Admin> pageInfo = new PageInfo<Admin>(alist);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("删除失败，请重试");
		}
		// pageNum代表页码值，pageSize代表每页条数
		PageHelper.startPage(pageNum, pageSize);

		return alist;
	}

	public List<Admin> findAdminListBy(Admin admin) {

		return null;
	}

	public List<Admin> selectAllAdmin() {
		List<Admin> alist = adminDao.selectAllAdmin();
		return alist;
	}

	//通过用户名和邮箱找回密码
	
	@Override
	public void findAdminByUE(String apassword,String email) {
		try {
			adminDao.findAdminByUE(apassword, email);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CustomException("找回密码失败");
		}
	}
}
